package vic.sendreceive.utilities;

import java.util.Collection;

import vic.sendreceive.param.BaseParamDefinition;

import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class GamePackage {
	private String cmd;	
	private ISFSObject params;	
	public String paramToString(){
		return params.toJson();		
	}
	public GamePackage() {
		cmd = "";
		params = new SFSObject();
	}
	public GamePackage(String cmd){
		this.cmd = cmd;
		params = new SFSObject();
	}
	public GamePackage(ISFSObject jsobj){		
		this.cmd = jsobj.getUtfString(BaseParamDefinition.cmd);			
		this.params = jsobj.getSFSObject(BaseParamDefinition.parameters);				
	}
	public GamePackage(String cmd, ISFSObject params){		
		this.cmd = cmd;	
		this.params = params;					
	}	
		
	public void setCommand(String cmd) {
		this.cmd = cmd;
	}
	
	public String getCommand() {
		return cmd;		
	}

	public void addParam(String key, String param){
		params.putUtfString(key,param);			
	}
	
	public void addParam(SFSObject params){
		this.params = params;
	}
	
	public void removeParam(String key){		
		params.removeElement(key);					
	}
	
	public String getParam(String key){		
		String result = null;
		String keyEncrypt = Encryption.Encrypt2(key);
		if(params.containsKey(keyEncrypt)){			
			result = Encryption.Decrypt2(params.getUtfString(keyEncrypt));
		}else{
			result = params.getUtfString(Encryption.Encrypt2(key).trim());
			result = Encryption.Decrypt2(result.toString()).trim();
		}		
		return result;
	}
	public Collection<Integer> getIntArrayParam(String key){		
		Collection<Integer> result = params.getIntArray(key);
		return result;
	}
	public SFSObject packCommand() {		
		SFSObject command = null;		    
		command = new SFSObject();		
		command.putUtfString(BaseParamDefinition.cmd, Encryption.Encrypt2(this.cmd));
		command.putUtfString(BaseParamDefinition.parameters, Encryption.Encrypt2(this.params.toJson()));		
		return command;
	}	
}
